(ns pe.p23
  (:use pe.digits)
  (:use clojure.set)
  (:use clojure.contrib.duck-streams))

(def len 28123)

(def statuses (boolean-array len))

(defn next-abundant [n]
  (let [next-n (inc n)]
    (if (abundant? next-n)
      next-n
      (recur next-n))))

(defn abundant-seq []
  (iterate next-abundant 12))

(defn abs-less-than [n]
  (take-while #(< % n) (abundant-seq)))

(defn set-contains? [x set]
  (not (nil? (set x))))

(def set-not-contains? (complement set-contains?))

(defn set-statuses [abundants]
  (doseq [x abundants y abundants
        :when (<= (+ x y) len)]
    (aset statuses (dec (+ x y)) true)))

(defn ab-sum []
  (let [candidates (abs-less-than (inc len))]
    (set-statuses candidates)))

(println (do
  (ab-sum)
  (reduce + (for [idx (range 0 (dec len)) :when (false? (aget statuses idx))] (inc idx)))))

;4179871